package server;

import java.io.*;
import java.text.*;
import java.util.Date;

import test.Main;

public class Logger {
	
	File f;
	Writer writer;
	
	public Logger() {
		this.f = new File("log.txt");
		try {
		PrintWriter out = new PrintWriter(f);
		out.print("Maze.net log file:");
		out.close();
		} catch(Exception e) {
			System.out.println(e);
		}
	}
	
	public void print(String text) {
		Main.echo(text);
		DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		Date date = new Date();
		text = dateFormat.format(date) + " - " + text;
		write(text);
	}
	
	public void print(Exception ex) {
		String text = ex.toString();
		Main.echo(text);
		DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
		Date date = new Date();
		text = dateFormat.format(date) + " - Error occured:";
		write(text);
		for(StackTraceElement str : ex.getStackTrace()) {
			write("                    " + str.toString());
		}
	}
	
	void write(String text) {
		try {
			BufferedWriter writer = new BufferedWriter(
			new FileWriter("log.txt",true));
			writer.newLine();
			writer.write(text);
			writer.close();
		} catch(Exception e) {
			System.out.println(e);
		} 
	}
	
}
